// pages/setStore/setStore.js
//获取应用实例
const app = getApp();
const api = require('../../utils/api');
const constant = require('../../utils/constant');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    //获取全局变量 导航栏的高度statusBarHeight
    statusBarHeight: app.globalData.statusBarHeight,
    // 胶囊
    jiaonang: app.globalData.jiaonang, 
    navigationHeight: app.globalData.navigationHeight,
    BASEURLWWW:constant.BASEURLWWW,
    storeDetailsData: {}
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.info(options)
    if(options.id){
      this.getStoreDetaislData(options.id)
    }
  },
  // 返回上一级
  back:function(){
    wx.reLaunch({
      url: '/pages/index/index',
    })
  },
  // 获取仓库详情数据
  getStoreDetaislData(id){
    let that = this
    api.storeDetails(id).then(res => {
      that.setData({
        storeDetailsData: res.data.data
      })
    })
  },
  // 上传图片
  uploadImg(e){
    let that = this
    wx.chooseImage({
      count: 1,
      sizeType: ['original', 'compressed'],
      sourceType: ['album', 'camera'],
      success (res) {
        wx.uploadFile({
          url: `${that.data.BASEURL}/myfile/wechat`, //仅为示例，非真实的接口地址
          filePath: res.tempFilePaths[0],
          name: 'file',
          header:{
            'Authorization': 'Bearer '+ wx.getStorageSync(storage.LOCAL_JWT_KEY),
            'Deviceid': 'loveqing'
          },
          formData: {},
          success (res){
            let jsonObj = JSON.parse(res.data)
            if(jsonObj.code == 0){
              wx.showToast({
                title: '上传成功',
                icon: 'success'
              })
              that.setData({
                'storeDetailsData.avatar_url': jsonObj.data.uri
              })
              that.updateStore()
            }else{
              wx.showToast({
                title: jsonObj.msg,
                icon: 'error'
              })
            }
            console.log(jsonObj);
          }
        })
      }
    })
  },
  // 修改仓库名字
  updateStoreName(e){
    this.setData({
      'storeDetailsData.store_name':e.detail.value
    })
    this.updateStore()
  },
  // 修改仓库地址
  updatestoreAddress(e){
    this.setData({
      'storeDetailsData.store_address':e.detail.value
    })
    this.updateStore()
  },
  // 修改仓库备注
  updatestoreRemark(e){
    this.setData({
      'storeDetailsData.remark':e.detail.value
    })
    this.updateStore()
  },
  // 修改仓库
  updateStore(){
    let that = this
    let {store_id,avatar_url,store_name,store_address,remark} = this.data.storeDetailsData
    let storeDetailsData = {store_id,avatar_url,store_name,store_address,remark}
    api.updateStore(storeDetailsData).then(res => {
      that.setData({
        storeDetailsData: {}
      })
      this.getStoreDetaislData(storeDetailsData.store_id)
      wx.showToast({
        title: '修改成功',
        icon: 'success'
      })
    })
  },
  // 删除仓库
  deleteStore(){
    let store_id = this.data.storeDetailsData.store_id.toString()
    wx.showModal({
      title: '提示',
      content: '确定删除该仓库？',
      success (res) {
        if (res.confirm) {
          api.deleteStore({store_id}).then(res => {
            wx.showToast({
              title: '成功',
            })
            setTimeout(()=>{
              wx.reLaunch({
                url: '/pages/storeIndex/storeIndex',
              })
            },500)
          })
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })    
  },
  // 跳转到店员权限设置
  jumpToClerkAuth:function(){
    wx.reLaunch({
      url: '/pages/clerkAuth/clerkAuth',
    })
  },
  // 跳转到添加店员
  jumpToAddClerk:function(){
    wx.reLaunch({
      url: '/pages/addClerk/addClerk',
    })
  }
})